﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manage
{
   
    public partial class Main : Form
    {

        public Main()
        {
            InitializeComponent();
            ThisLink.cRequst.Start();
            ThisLink.cRequst.dLogout += HLogout;
            login = new Login();
            login.GetAllKey += GetAllKey;
            login.ShowDialog();
        }

        public void GetAllKey()
        {

         //   this.Invoke(new MethodInvoker(() => {
                lbKeys.Items.Clear();
                var listKey = ThisLink.cRequst.MGetAllKey();
                lbKeys.Items.Clear();
                if (listKey != null)
                {
                    foreach (var item in listKey)
                    {
                        lbKeys.Items.Add(item);
                    }
                }
          //  }));
          
        }
         ~Main()
        {
            Application.Exit();
        }

        Login login;
        public void HLogout()
        {
            MessageBox.Show(ThisLink.cRequst.HeartbeadtMessage);
            login.Dispose();
            login = null;
          
                login = new Login();
                login.GetAllKey += GetAllKey;
                login.ShowDialog();
         
           
        }
        private void Login_Load(object sender, EventArgs e)
        {
          
        }
        private void lbKeys_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = lbKeys.SelectedItem;
            if (item!=null)
            {
                Dictionary<string,string> result= ThisLink.cRequst.GetKeyValue(item.ToString());
                if (result["status"]=="")
                {
                    tbValue.Text = result["value"];
                    tbNote.Text = result["note"];
                }
                else
                {
                    MessageBox.Show(result["message"]);
                }
            }
        
        }
        private void btAdd_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> result = ThisLink.cRequst.AddKeyValue(tbNewKey.Text.Trim());
            if (result["status"]=="")
            {
                GetAllKey();
            }
            MessageBox.Show(result["message"]);
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> result = ThisLink.cRequst.UpdateKeyValue(lbKeys.SelectedItem.ToString(),tbValue.Text.Trim(),tbNote.Text.Trim());
            MessageBox.Show(result["message"]);
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            GetAllKey();
        }

        private void btCreateCdkey_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> p = new Dictionary<string, string>();
            p.Add("script_id",cmbScriptIdName.Text);
            p.Add("value",cmbValue.Text);
            p.Add("num",cmbNum.Text);
            p.Add("note", tbCdKeyNote.Text.Trim());
            Dictionary<string, string> result = ThisLink.cRequst.CreateCDKey(p);
            MessageBox.Show(result["message"]);

        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> p = new Dictionary<string, string>();
            if (cbFScriptId.Checked)
            {
                p.Add("script_id",cmbScriptId.Text);
            }
            if (cbCreateTime.Checked)
            {
                  p.Add("create_time_start",dtpFCreateTimeStart.Value.Date.ToString());
                  p.Add("create_time_end", dtpFCreateTimeEnd.Value.Date.AddDays(1).AddMilliseconds(-1).ToString());
            }
            if (cbFUseTime.Checked)
            {
                p.Add("use_time_start", dtpFUseTimeStart.Value.Date.ToString());
                p.Add("use_time_end", dtpFUseTimeEnd.Value.Date.AddDays(1).AddMilliseconds(-1).ToString());
            }
            if (cbFValue.Checked)
            {
                p.Add("value",cmbFValue.Text.Trim());
            }
            if (cbFUserName.Checked)
            {
                p.Add("user_name", tbFUserName.Text.Trim());
            }
            if (cbUserId.Checked)
            {
                p.Add("user_id",tbFUserId.Text.Trim());
            }
            if (cbIsUse_check.Checked)
            {
                p.Add("is_use",cbIsUse.Checked.ToString().ToLower());
            }
            if (cbCdkey.Checked)
            {
                p.Add("cdkey", tbFCdkey.Text.Trim());
            }
            if (cbFNote.Checked)
            {
                p.Add("note", tbFNote.Text.Trim());
            }
            dgvCDKEY.DataSource = ThisLink.cRequst.GetCDKeys(p);
           
        }

        private void t1Seach_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> p = new Dictionary<string, string>();
            if (Tab1cbScriptId.Checked)
            {
                p.Add("script_id", Tab1cmbScriptId.Text.Trim());
            }
            if (t1cbCheckRegiserTime.Checked)
            {
                p.Add("create_time_start", t1DtpRegisterTimeStart.Value.Date.ToString());
                p.Add("create_time_end", t1DtpRegisterTimeEnd.Value.Date.AddDays(1).AddMilliseconds(-1).ToString());
            }
            if (t1cbTpexpirationTime.Checked)
            {
                p.Add("expiration_time_start", t1DtpExpirationTimeStart.Value.Date.ToString());
                p.Add("expiration_time_end", t1DtpExpirationTimeEnd.Value.Date.AddDays(1).AddMilliseconds(-1).ToString());
            }
            
            if (t1cbCheckUserName.Checked)
            {
                p.Add("user_name", t1txtUserName.Text.Trim());
            }
            if (t1cbCheckUserId.Checked)
            {
                p.Add("user_id", t1cbCheckUserId.Text.Trim());
            }
            if (t1cbCheckOnline.Checked)
            {
                p.Add("is_online", t1cbOnline.Checked.ToString().ToLower());
            }
            DGVOnlineUser.DataSource = ThisLink.cRequst.GetUserList(p);
        }

        private void trLoadData_Tick(object sender, EventArgs e)
        {
            Dictionary<string, string> p = new Dictionary<string, string>();
            p.Add("is_online", t1cbOnline.Checked.ToString().ToLower());
            ThisLink.cRequst.GetUserList(p);
        }

        
    }
}
